<?php
require_once "interface/Gi_Paginator_Adapter_Interface.php";
class Gi_Paginator implements Countable, IteratorAggregate {
  private $_start_row = 1;
  
  private $_rows_per_page = 10;
  
  private $_current_page = 1;
  
  private $_adapter = null;
  
  private $_current_items = null;
/*  
Propiedad             Tipo        Descripcion
first                 integer     Numero de la primera pagina (i.e., 1) 
first_item_number     integer     numero del primer item de la pagina
current               integer     Numero de pagina 
last                  integer     Numero de la ultima pagina
last_item_number      integer     Numero del ultimo item de la pagina
next                  integer     Numero de la proxima pagina
page_count            integer     Numero de paginas totales
previous              integer     Numero de la pagina anterior
total_item_count      integer     Numero total de items
*/
  public $first               = 1;
  public $first_item_number   = 1;
  public $current             = 1;
  public $last                = 1;
  public $last_item_number    = 1;
  public $next                = 1;
  public $page_count          = 1;
  public $previous            = 1;
  public $total_item_count    = 1;

  
  public function __construct ($dataset, $rows_per_page = null) {
    if (is_string($dataset)) {
      $this->_adapter = new Gi_Paginator_Select($dataset);
    }
    else if (is_array($dataset)) {
      $this->_adapter = new Gi_Paginator_Array($dataset);
    }
    else if ($dataset instanceof Gi_Sql_Writer) {
      $this->_adapter = new Gi_Paginator_Select($dataset->__toString());
    }
    else {
      throw new Gi_Exception("No hay adaptador disponible");
    }
    
    if (is_int($rows_per_page)) {
      $this->_rows_per_page = $rows_per_page;
    }
    
    $this->last = count($this); 
    
    if (count($this->_adapter) > $this->_rows_per_page) {
      $this->last_item_number = $this->_rows_per_page;
    }
    else {
      $this->last_item_number = count($this->_adapter);
    }
    
    $this->next = $this->current + 1;
    $this->page_count = count($this);
    $this->previous = null;
    $this->total_item_count = count($this->_adapter);
    return $this;
  }
  
  /*Metodos de la interface Countable*/ 
  /*devuelve la cantidad de paginas del paginador*/
  public function count () { 
    $items = count($this->_adapter);
    $pages = ceil($items / $this->_rows_per_page);
    return $pages;
  }
  
  /*Metodos de la interface iteratorAgregate deve devolver algo compatible con un foreach IE array*/
  public function getIterator () {
    return $this->get_current_items ();
  }  
  
  public function get_items_by_page ($page) {
    $this->_current_page = $page;
    $this->_start_row = ($this->_rows_per_page * $page) - ($this->_rows_per_page);
    
    $this->first_item_number   = $this->_start_row;
    $this->current             = $this->_current_page;
    $this->last_item_number    = $this->_current_page * $this->_rows_per_page;
    $this->next                = $this->_current_page + 1;
    $this->previous            = $this->_current_page - 1;
    $this->_current_items = $this->_adapter->get_items($this->_start_row,$this->_rows_per_page);
    return new ArrayObject($this->_current_items);
  }
  
  public function get_current_items () {
    return $this->get_items_by_page($this->_current_page);
  }
}
